package com.hrhx.duhongming.scheduled;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;

import com.hrhx.duhongming.socket.GaugeDataWebSocket;
import com.hrhx.duhongming.socket.LineDataWebSocket;
import com.hrhx.duhongming.utils.JsonUtil;

@Component
public class DataScheduledTask{
	
	@Autowired
	private LineDataWebSocket lineDataWebSocket;
	@Autowired
	private GaugeDataWebSocket gaugeDataWebSocket;
	
	@Scheduled(cron="0/2 * *  * * ? ")   //每2秒执行一次
	public void getGaugeDataScheduledTask(){
		Random random = new Random();
		Double d = random.nextDouble()*100;
		DecimalFormat df = new DecimalFormat("######0.00");
		gaugeDataWebSocket.sendMessageToUsers(new TextMessage(df.format(d)));
		//System.out.println("我是DataScheduledTask,getGaugeDataScheduledTask()正在发送后台推送数据："+jsonStr);
		
	}
	
	@Scheduled(cron="0/2 * *  * * ? ")   //每2秒执行一次
	public void getLineDataScheduledTask(){
			Random random = new Random();
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Map<String,String> map = new HashMap<String,String>();
			map.put("最低气温",String.valueOf(random.nextInt(10)));
			map.put("最高气温",String.valueOf(random.nextInt(10)));
			map.put("xxx",sdf.format(date));
			String jsonData = new JsonUtil().toStrings(map);
			lineDataWebSocket.sendMessageToUsers(new TextMessage(jsonData));
		//System.out.println("我是DataScheduledTask,getLineDataScheduledTask()正在发送后台推送数据："+jsonData);
	}


}
